﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Yipusoft.BaseDoc.Model;

public partial class Task_BaseDoc_DocumentFileNew : System.Web.UI.Page
{
    List<DocumentFileInfo> gList = new List<DocumentFileInfo>();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            SetDate();
            dptStartYear.SelectedValue = DateTime.Now.Year.ToString();
            dptStartMonth.SelectedValue = DateTime.Now.Month.ToString();

            if (Request["id"] != null)
            {
                DocumentFileInfo doc = Yipusoft.BaseDoc.BLL.FileManager.GetFile(new Guid(Request["id"]));
                if (doc.IsFolder)
                {
                    dptStartMonth.SelectedValue = doc.StartDate.Value.Month.ToString();
                    dptStartYear.SelectedValue = doc.StartDate.Value.Year.ToString();
                }
                else
                {
                    doc = Yipusoft.BaseDoc.BLL.FileManager.GetFile(doc.ParentID);
                    dptStartMonth.SelectedValue = doc.StartDate.Value.Month.ToString();
                    dptStartYear.SelectedValue = doc.StartDate.Value.Year.ToString();
                }
            }
            Bind();
            if (Request["id"] != null)
            {
                DocumentFileInfo doc = Yipusoft.BaseDoc.BLL.FileManager.GetFile(new Guid(Request["id"]));
                if (doc.IsFolder)
                {
                    dptParent.SelectedValue = Request["id"];
                }
                else
                {
                    dptParent.SelectedValue = doc.ParentID.ToString();
                }

            }
        }
    }
    private void Bind()
    {
        DateTime startDate = new DateTime(int.Parse(dptStartYear.SelectedValue), int.Parse(dptStartMonth.SelectedValue), 1);
        dptParent.Items.Clear();
        string DepartmentID = Request["DepartmentID"];
        List<Yipusoft.BaseDoc.Model.DocumentFileInfo> list = Yipusoft.BaseDoc.BLL.FileManager.GetList(" IsFolder='True' And DepartmentID='" + DepartmentID + "' And ParentID='" + Guid.Empty + "' and year(startDate)=" + dptStartYear.SelectedValue + " and month(startDate)=" + dptStartMonth.SelectedValue + "  Order by OrderID ");
        foreach (DocumentFileInfo file in list)
        {
            dptParent.Items.Add(new ListItem("".PadLeft(2 * file.Level.Value, '　') + file.OldFileName, file.ID.ToString()));
            OrderItem(file.ID, file.OldFileName);



        }
        dptParent.Items.Insert(0, new ListItem("顶级分类", "00"));

    }
    private void OrderItem(Guid ID, string oldFileName)
    {
        List<Yipusoft.BaseDoc.Model.DocumentFileInfo> list = Yipusoft.BaseDoc.BLL.FileManager.GetList(" IsFolder='True'  And ParentID='" + ID + "' Order by OrderID ");
        if (list != null)
        {
            foreach (DocumentFileInfo info in list)
            {
                dptParent.Items.Add(new ListItem("".PadLeft(2 * info.Level.Value, '　') + info.OldFileName, info.ID.ToString()));
                OrderItem(info.ID, info.OldFileName);
            }
        }

    }
    private void SetDate()
    {
        for (int i = 2009; i <= 2020; i++)
        {
            dptStartYear.Items.Add(new ListItem(i.ToString() + "年", i.ToString()));
            //dptEndYear.Items.Add(new ListItem(i.ToString() + "年", i.ToString()));
        }
        for (int i = 1; i <= 12; i++)
        {
            dptStartMonth.Items.Add(new ListItem(i.ToString() + "月", i.ToString()));
            //dptEndMonth.Items.Add(new ListItem(i.ToString() + "月", i.ToString()));
        }
        for (int i = 0; i <= 50; i++)
        {
            dptOrder.Items.Add(new ListItem(i.ToString(), i.ToString()));
        }
    }
    protected void btnAdd_Click(object sender, EventArgs e)
    {
        lblPrompt.Text = "";
        bool bl = false;
        DocumentFileInfo file = new DocumentFileInfo();
        file.ID = Guid.NewGuid();
        file.IsFolder = true;
        file.OldFileName = txtFile.Text;
        file.NewFileName = txtFile.Text;
        file.HasChild = false;
        if (dptParent.SelectedValue.Equals("00"))
        {
            file.ParentID = Guid.Empty;
            file.Level = 0;
        }
        else
        {
            bl = true;
            file.ParentID = new Guid(dptParent.SelectedValue);
            file.Level = Yipusoft.BaseDoc.BLL.FileManager.GetFile(new Guid(dptParent.SelectedValue)).Level + 1;
        }
        file.LastAccessDate = DateTime.Now;
        file.CreateDate = DateTime.Now;
        file.CreateUser = User.Identity.Name;
        file.FileSize = 0;
        file.FilePath = "";
        file.OrderID = int.Parse(dptOrder.SelectedValue);
        file.DepartmentID = Request["DepartmentID"];
        file.Format = "Folder";
        file.StartDate = new DateTime(int.Parse(dptStartYear.SelectedValue), int.Parse(dptStartMonth.SelectedValue), 1);
        //file.EndDate = new DateTime(int.Parse(dptEndYear.SelectedValue), int.Parse(dptEndMonth.SelectedValue), 1);
        lblPrompt.Text = Yipusoft.BaseDoc.BLL.FileManager.AddFile(file) ? "添加成功" : "添加失败";
        Bind();
        if (bl)
            dptParent.SelectedValue = file.ParentID.ToString();

    }
    protected void bthParent_Click(object sender, EventArgs e)
    {
        Response.Redirect("DocumentList.aspx");
    }
    protected void dptStartMonth_SelectedIndexChanged(object sender, EventArgs e)
    {
        Bind();
    }
}
